package io.renren.modules.store.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.common.annotation.NoRepeatSubmit;
import io.renren.common.exception.RRException;
import io.renren.common.utils.BeanJudgeUtil;
import io.renren.common.utils.ReplaceUtil;
import io.renren.modules.store.entity.TStoreFoodEntity;
import io.renren.modules.store.service.TStoreFoodService;
import io.renren.modules.sys.controller.AbstractController;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.modules.store.entity.TStoreFoodTypeEntity;
import io.renren.modules.store.service.TStoreFoodTypeService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 * 
 *
 * @author steakliu
 * @email 2319492349@qq.com
 * @date 2021-01-24 17:20:12
 */
@RestController
@RequestMapping("store/tstorefoodtype")
public class TStoreFoodTypeController extends AbstractController {
    @Autowired
    private TStoreFoodTypeService tStoreFoodTypeService;
    @Autowired
    private TStoreFoodService storeFoodService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("store:tstorefoodtype:list")
    public R list(@RequestParam Map<String, Object> params){
        params.put("sellerId",getSeller());
        PageUtils page = tStoreFoodTypeService.queryPage(params);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("store:tstorefoodtype:info")
    public R info(@PathVariable("id") Long id){
		TStoreFoodTypeEntity tStoreFoodType = tStoreFoodTypeService.getById(id);
        String useRange = tStoreFoodType.getUseRange();
        List<Long> list = new ArrayList<>();
        if (useRange == null){
            list.add(null);
        }
        else if (useRange.length() == 1){
            list.add(Long.valueOf(useRange));
        }else{
            String[] split = useRange.split(",");
            for (String s : split) {
                list.add(Long.valueOf(s));
            }
        }
        tStoreFoodType.setUseRangList(list);
        return R.ok().put("tStoreFoodType", tStoreFoodType);
    }

    /**
     * 保存
     */
    @NoRepeatSubmit
    @RequestMapping("/save")
    @RequiresPermissions("store:tstorefoodtype:save")
    public R save(@RequestBody TStoreFoodTypeEntity tStoreFoodType){
        TStoreFoodTypeEntity one = tStoreFoodTypeService.getOne(new QueryWrapper<TStoreFoodTypeEntity>()
                .eq("seller_id", getSeller())
                .eq("classification_no", tStoreFoodType.getClassificationNo())
                .or()
                .eq("classification_name", tStoreFoodType.getClassificationName())
        );
        if (one != null){
            throw new RRException("菜品类型或编号存在相同项，请重新输入");
        }
        tStoreFoodType.setSellerId(getSeller());
		tStoreFoodTypeService.save(tStoreFoodType);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("store:tstorefoodtype:update")
    public R update(@RequestBody TStoreFoodTypeEntity tStoreFoodType){
		tStoreFoodTypeService.updateById(tStoreFoodType);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("store:tstorefoodtype:delete")
    public R delete(@RequestBody Long[] ids){
        TStoreFoodEntity one = storeFoodService.getOne(new QueryWrapper<TStoreFoodEntity>()
                .eq("seller_id", getSeller())
                .eq("type_id", ids[0])
        );
        if (one != null){
            throw new RRException("此菜品类型下存在菜品，无法删除");
        }
        tStoreFoodTypeService.removeByIds(Arrays.asList(ids));
        return R.ok();
    }

}
